//
//  ZGL24ClsDetailVC.m
//  Faker
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 dllo. All rights reserved.
//

#define Address @"http://c.3g.163.com/nc/article/list/%@/%lu-20.html"//请求地址

#import "ZGL24ClsDetailVC.h"
#import "ZGL24ClsDetailCell.h"
#import "ZGL24ClsDetailModel.h"

#import <MJRefresh.h>
#import "ZGLHTTPTOOL.h"
#import "ZGLHotDetailsVC.h"
@interface ZGL24ClsDetailVC ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *hotView;
@property(nonatomic, retain)NSMutableArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@end

@implementation ZGL24ClsDetailVC
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        self.modArr = [[NSMutableArray alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBarButtonItem:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    [self createTableView];
}
- (void)leftBarButtonItem:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor = backColor;
    _hotView.backgroundColor = backColor;
}

//创建tableview
- (void)createTableView{
    _hotView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    _hotView.delegate = self;
    _hotView.dataSource = self;
    [_hotView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_hotView];
    
    //    注册cell
    [_hotView registerClass:[ZGL24ClsDetailCell class] forCellReuseIdentifier:@"ZGL24ClsDetailCell"];
    
    //    刷新和加载
    _hotView.tableFooterView = [[UIView alloc]init];
    __weak typeof(self) weakSelf = self;
    _hotView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [weakSelf handelDataWithURL:[NSString stringWithFormat:Address, _tid, (unsigned long)0] WithType:0];
    }];
    
    _hotView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf handelDataWithURL:[NSString stringWithFormat:Address, _tid, (unsigned long)_modArr.count] WithType:1];
    }];
    [_hotView.header beginRefreshing];
}

// MJRefrash请求数据
- (void)handelDataWithURL:(NSString *)url WithType:(NSInteger)type{
    __weak typeof(self) weakSelf = self;
    [ZGLHTTPTOOL GETWithURL:url withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        if (type == 0) {
            _modArr = [[NSMutableArray alloc]initWithArray:[tempDic objectForKey:_tid]];
        }else if (type == 1){
            [_modArr addObjectsFromArray:[tempDic objectForKey:_tid]];
        }
        [weakSelf.hotView reloadData];
        [weakSelf.hotView.header endRefreshing];
        [weakSelf.hotView.footer endRefreshing];
    }withFail:^(NSError *error) {
        NSLog(@"error:没网 ZGLHotVC");
        [weakSelf.hotView.header endRefreshing];
        [weakSelf.hotView.footer endRefreshing];
    }];
}

//判断cell种类
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGL24ClsDetailModel *mod = [ZGL24ClsDetailModel modWithDic:self.modArr[indexPath.row]];
    ZGL24ClsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier: @"ZGL24ClsDetailCell"];
    cell.mod = mod;
    self.cellheight = cell.height;
    return cell;
}

//动态设置cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

//cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.view.userInteractionEnabled = NO;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZGLHotDetailsVC *hotDetails = [[ZGLHotDetailsVC alloc]init];
    hotDetails.docid = [self.modArr[indexPath.row]objectForKey:@"docid"];
    [self.navigationController pushViewController:hotDetails animated:YES];
    self.view.userInteractionEnabled = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
